package com.flyco.modules.system.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.mapper.StmStoreInfoMapper;
import com.flyco.modules.system.entity.SysUser;
import com.flyco.modules.system.entity.SysUserStore;
import com.flyco.modules.system.mapper.SysUserMapper;
import com.flyco.modules.system.mapper.SysUserStoreMapper;
import com.flyco.modules.system.model.StoreInfoModel;
import com.flyco.modules.system.service.ISysUserStoreService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 人员经销商关系表
 * @Author: flyco
 * @Date: 2024-07-30
 * @Version: V1.0
 */
@Service
public class SysUserStoreServiceImpl extends ServiceImpl<SysUserStoreMapper, SysUserStore> implements ISysUserStoreService {

    @Resource
    private StmStoreInfoMapper stmStoreInfoMapper;
    @Resource
    private SysUserStoreMapper sysUserStoreMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public List<StoreInfoModel> getStore(String channelIds, String storeName, Long partnerId, String officeIds) {
        QueryWrapper<StmStoreInfo> query = new QueryWrapper<>();
        query.eq("status", 9);
        if (StringUtils.isNotEmpty(channelIds)) {
            String[] channelIdList = channelIds.trim().split(",");
            query.in("channel_id", channelIdList);
        }
        if (StringUtils.isNotEmpty(officeIds)) {
            String[] officeIdList = officeIds.trim().split(",");
            query.in("group_id", officeIdList);
        }
        query.like("name", storeName);
        query.eq("partner_id", partnerId);
        List<StoreInfoModel> storeModelList = new ArrayList<>();
        List<StmStoreInfo> storeInfoList = stmStoreInfoMapper.selectList(query);
        if (!storeInfoList.isEmpty()) {
            storeInfoList.forEach(x -> {
                StoreInfoModel model = new StoreInfoModel();
                model.setChannelId(x.getChannelId());
                model.setChannelName(x.getChannelName());
                model.setStoreId(x.getStoreId().toString());
                model.setStoreName(x.getName());
                model.setStoreCode(x.getCompanyCode());
                model.setPartnerId(x.getPartnerId());
                storeModelList.add(model);
            });
        }
        return storeModelList;
    }

    @Override
    public SysUserStore getByUserId(String id, Long partnerId) {
        SysUserStore userStore = new SysUserStore();
        List<StoreInfoModel> storeList = new ArrayList<>();
        SysUser user = sysUserMapper.selectById(id);
        if (user != null) {
            userStore.setSalemanName(user.getRealname());
            userStore.setSalemanId(user.getUsername());
            QueryWrapper<SysUserStore> query = new QueryWrapper<>();
            query.eq("saleman_id", user.getUsername());
            query.eq("partner_id", partnerId);
            List<SysUserStore> userStoreList = sysUserStoreMapper.selectList(query);
            if (userStoreList != null && !userStoreList.isEmpty()) {
                List<String> storeCodeList = userStoreList.stream().map(x -> x.getStoreId()).collect(Collectors.toList());
                QueryWrapper<StmStoreInfo> storeQuery = new QueryWrapper<>();
                storeQuery.in("company_code", storeCodeList);
                storeQuery.eq("partner_id", partnerId);
                List<StmStoreInfo> storeInfoList = stmStoreInfoMapper.selectList(storeQuery);
                if (storeInfoList != null && !storeInfoList.isEmpty()) {
                    storeInfoList.forEach(x -> {
                        StoreInfoModel model = new StoreInfoModel();
                        model.setChannelId(x.getChannelId());
                        model.setChannelName(x.getChannelName());
                        model.setStoreId(x.getStoreId().toString());
                        model.setStoreName(x.getName());
                        model.setStoreCode(x.getCompanyCode());
                        model.setPartnerId(x.getPartnerId());
                        storeList.add(model);
                    });
                }
            }
        }
        userStore.setStoreInfoList(storeList);
        return userStore;
    }

    @Override
    public void saveWithStore(SysUserStore sysUserStore) {
        if (sysUserStore.getStoreInfoList() != null && !sysUserStore.getStoreInfoList().isEmpty()) {
            QueryWrapper<SysUserStore> query = new QueryWrapper<>();
            query.eq("saleman_id", sysUserStore.getSalemanId());
            query.eq("partner_id", sysUserStore.getPartnerId());
            List<SysUserStore> userStoreList = sysUserStoreMapper.selectList(query);
            if (userStoreList != null && !CollectionUtils.isEmpty(userStoreList)) {
                userStoreList.forEach(x -> {
                    //如果新集合里面不存在删除老数据
                    List<StoreInfoModel> tempList = sysUserStore.getStoreInfoList().stream().filter(t -> t.getStoreCode().equals(x.getStoreId()) && sysUserStore.getSalemanId().equals(x.getSalemanId())).collect(Collectors.toList());
                    if (tempList == null || tempList.isEmpty()) {
                        sysUserStoreMapper.deleteById(x.getId());
                    }
                });
            }
            //新增不存在的关系
            sysUserStore.getStoreInfoList().forEach(x -> {
                List<SysUserStore> tempList = userStoreList.stream().filter(t -> t.getStoreId().equals(x.getStoreCode()) && t.getSalemanId().equals(sysUserStore.getSalemanId())).collect(Collectors.toList());
                if (tempList == null || tempList.isEmpty()) {
                    SysUserStore userStore = new SysUserStore();
                    userStore.setSalemanId(sysUserStore.getSalemanId());
                    userStore.setSalemanName(sysUserStore.getSalemanName());
                    userStore.setStoreId(x.getStoreCode());
                    userStore.setStoreName(x.getStoreName());
                    userStore.setCreateTime(new Date());
                    userStore.setPartnerId(x.getPartnerId());
                    sysUserStoreMapper.insert(userStore);
                }
            });
        }
    }
}
